<template>
  <div class="page-container" v-global-loading="loading">
    <div class="page-card-container">
      <a-tabs
        v-model:activeKey="activeKey"
        @change="changeTab"
        v-if="pageType == 2"
      >
        <a-tab-pane :key="1" tab="待我审核"> </a-tab-pane>
        <a-tab-pane :key="2" tab="我已审核"> </a-tab-pane>
        <a-tab-pane :key="3" tab="抄送于我"> </a-tab-pane>
      </a-tabs>
      <a-form :model="pageSet" layout="inline">
        <a-form-item label="会员名称">
          <a-input
            v-model:value="pageSet.name"
            style="width: 170px"
            :allowClear="true"
          ></a-input>
        </a-form-item>
        <a-form-item label="会员手机号">
          <a-input
            v-model:value="pageSet.phone"
            style="width: 170px"
            :allowClear="true"
          ></a-input>
        </a-form-item>
        <a-form-item label="会员标签">
          <a-select
            v-model:value="pageSet.tag"
            style="width: 230px"
            :filter-option="filterOption"
            show-search
          >
            <a-select-option
              :value="item.name"
              :title="item.name"
              v-for="item in tagDataList"
              >{{ item.name }}</a-select-option
            >
          </a-select>
        </a-form-item>
        <a-form-item label="会员等级">
          <a-select
            v-model:value="pageSet.levelId"
            style="width: 230px"
            :filter-option="filterOption"
            show-search
          >
            <a-select-option
              :value="item.id"
              :title="item.name"
              v-for="item in userList"
              >{{ item.name }}</a-select-option
            >
          </a-select>
        </a-form-item>
        <a-form-item label="公司名称">
          <a-input
            v-model:value="pageSet.parentCompany"
            style="width: 170px"
            :allowClear="true"
          ></a-input>
        </a-form-item>
        <a-form-item label="会员等级申请状态">
          <a-select
            v-model:value="pageSet.ApplyStatus"
            style="width: 230px"
            allowClear
          >
            <a-select-option
              :value="item.value"
              :title="item.label"
              v-for="(item, index) in applyStatusOptions"
              :key="index"
              >{{ item.label }}</a-select-option
            >
          </a-select>
        </a-form-item>
        <a-form-item label="">
          <div class="commonBtn-warning" @click="loadPagination(true)">
            查询
          </div>
        </a-form-item>
        <a-form-item label="">
          <div class="commonBtn-primary" @click="reset">重置</div>
        </a-form-item>
        <a-form-item label="">
          <div
            class="commonBtn-primary"
            @click="exportDerive"
            v-if="pageType == 1"
          >
            批量导出会员信息
          </div>
        </a-form-item>
      </a-form>
      <a-table
        size="small"
        :loading="tableLoading"
        :dataSource="tableData"
        :columns="columns"
        bordered
        :pagination="false"
        :scroll="{
          x: 1200,
          y: useAppStore.pageCommonInfo.canUseHeight - 36 + 'px'
        }"
      >
        <template #bodyCell="{text, record, index, column}">
          <template v-if="column.title == '会员标签'">
            <a-tooltip>
              <template #title>{{ text }}</template>
              <div class="font-left nowrap">{{ text }}</div>
            </a-tooltip>
          </template>
          <template v-if="column.title == '操作'">
            <span
              onselectstart="return false"
              class="pointer c-primaryColor"
              @click="compile(1, record.id)"
              >查看</span
            >
            <span
              onselectstart="return false"
              class="pointer c-primaryColor"
              @click="compile(9, record.id)"
              v-if="activeKey == 1 && pageType == 2"
              >&nbsp;&nbsp;审核</span
            >
            <span
              onselectstart="return false"
              class="pointer c-primaryColor"
              v-if="pageType == 1"
              @click="compile(2, record.id, record.status)"
              >&nbsp;&nbsp;{{ record.status == 1 ? "停用" : "启用" }}</span
            >
            <span
              onselectstart="return false"
              class="pointer c-primaryColor"
              v-if="pageType == 1"
              @click="compile(3, record.id)"
              >&nbsp;&nbsp;编辑&nbsp;&nbsp;</span
            >
            <a-dropdown v-if="pageType == 1">
              <span class="pointer">更多<more-outlined /></span>
              <template #overlay>
                <a-menu>
                  <a-menu-item key="1">
                    <span
                      onselectstart="return false"
                      class="pointer c-primaryColor"
                      @click="compile(4, record.id)"
                      >&nbsp;&nbsp;设置协议价</span
                    >
                  </a-menu-item>
                  <a-menu-item key="2" v-if="record.applyStatus != 1">
                    <span
                      onselectstart="return false"
                      class="pointer c-primaryColor"
                      @click="compile(5, record.id)"
                      >&nbsp;&nbsp;设置等级</span
                    >
                  </a-menu-item>
                  <a-menu-item key="3">
                    <span
                      onselectstart="return false"
                      class="pointer c-primaryColor"
                      @click="
                        compile(7, record.id, record.status, record.tagIds)
                      "
                      >&nbsp;&nbsp;设置标签</span
                    >
                  </a-menu-item>
                  <a-menu-item key="4">
                    <span
                      onselectstart="return false"
                      class="pointer c-primaryColor"
                      @click="compile(6, record.id)"
                      >&nbsp;&nbsp;授信设置</span
                    >
                  </a-menu-item>
                  <a-menu-item key="5">
                    <span
                      onselectstart="return false"
                      class="pointer c-primaryColor"
                      @click="compile(8, record.id)"
                      >&nbsp;&nbsp;导出会员协议信息</span
                    >
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </template>
          <template v-if="column.title == '会员等级申请状态'">
            <span v-if="text == 0" onselectstart="return false">未提交</span>
            <span
              v-else-if="text == 1"
              onselectstart="return false"
              class="pointer c-primaryColor"
              @click="compile(9, record.id)"
              >审核中</span
            >
            <span
              v-else-if="text == 2"
              onselectstart="return false"
              class="pointer c-primaryColor"
              @click="compile(9, record.id)"
              >审核驳回</span
            >
            <span
              v-else-if="text == 3"
              onselectstart="return false"
              class="pointer c-primaryColor"
              @click="compile(9, record.id)"
              >已撤回</span
            >
            <span
              v-else-if="text == 9 && record.flowDesignId"
              onselectstart="return false"
              class="pointer c-primaryColor"
              @click="compile(9, record.id)"
              >审核通过</span
            >
            <span v-else>审核通过</span>
          </template>
        </template>
      </a-table>
      <Pagination
        :pageSet="pageSet"
        style="margin-top: 10px"
        :loadPagination="loadPagination"
      ></Pagination>
      <!-- 详情 -->
      <a-modal
        v-model:open="visible"
        width="800px"
        :title="null"
        :footer="null"
        :maskClosable="false"
      >
        <div v-global-loading="modalloading">
          <a-tabs v-model:activeKey="activeKey2" @change="changeTab2">
            <a-tab-pane :key="1" :tab="title">
              <a-form :model="form" ref="formRef" :labelCol="{span: 8}">
                <a-row :gutter="24">
                  <a-col :span="12">
                    <a-form-item
                      label="会员名称"
                      name="name"
                      :rules="[{required: true, message: '请填写'}]"
                      v-if="examine || isupData"
                    >
                      <a-input v-model:value="form.name" allowClear />
                    </a-form-item>
                    <a-form-item
                      label="会员手机号"
                      name="phone"
                      v-if="examine || isupData"
                    >
                      <a-input v-model:value="form.phone" allowClear />
                    </a-form-item>
                    <a-form-item
                      label="账号"
                      name="account"
                      v-if="examine || isupData"
                    >
                      <a-input v-model:value="form.account" allowClear />
                    </a-form-item>
                    <a-form-item
                      label="客服姓名"
                      name="customerServiceName"
                      v-if="examine || isupData"
                    >
                      <a-input
                        v-model:value="form.customerServiceName"
                        allowClear
                      />
                    </a-form-item>
                    <a-form-item
                      label="会员等级"
                      name="levelId"
                      v-if="examine || isupData || isLevelId"
                    >
                      <a-select
                        v-model:value="form.levelId"
                        :disabled="!isLevelId"
                        @change="transfer1($event, 'levelId', 'levelName')"
                        allowClear
                        :filter-option="filterOption"
                        show-search
                      >
                        <a-select-option
                          :value="item.id"
                          :title="item.name"
                          v-for="item in userList"
                          @click="
                            transfer('levelId', 'levelName', item.id, item.name)
                          "
                          >{{ item.name }}</a-select-option
                        >
                      </a-select>
                    </a-form-item>
                    <!-- <a-form-item label="密码" name="passWord" v-if="examine || isupData">
                  <a-input v-model:value="form.passWord" allowClear disabled />
                </a-form-item> -->
                    <a-form-item
                      label="授信额度"
                      name="creditLine"
                      v-if="examine || isupData"
                    >
                      <a-input-number
                        style="width: 240px"
                        v-model:value="form.creditLine"
                        :controls="false"
                        disabled
                      />
                    </a-form-item>
                    <a-form-item
                      label="是否开启授信支付"
                      name="isCredit"
                      v-if="isCreditLine"
                    >
                      <a-radio-group
                        v-model:value="form.isCredit"
                        :disabled="!isCreditLine"
                        name="isCredit"
                      >
                        <a-radio :value="true">是</a-radio>
                        <a-radio :value="false">否</a-radio>
                      </a-radio-group>
                    </a-form-item>
                    <a-form-item
                      label="是否启用"
                      name="status"
                      v-if="examine || isupData"
                    >
                      <a-radio-group v-model:value="form.status" name="status">
                        <a-radio :value="1">是</a-radio>
                        <a-radio :value="0">否</a-radio>
                      </a-radio-group>
                    </a-form-item>
                    <a-form-item
                      label="计算方式"
                      v-if="
                        chooseProductsData.isAllProduct && (examine || isupData)
                      "
                    >
                      <a-radio-group
                        disabled
                        v-model:value="chooseProductsData.isDiscount"
                        name="isDiscount"
                      >
                        <a-radio :value="true">折扣</a-radio>
                        <a-radio :value="false">固定价</a-radio>
                      </a-radio-group>
                    </a-form-item>
                  </a-col>
                  <a-col :span="12">
                    <a-form-item
                      label="审批流程"
                      name="flowDesignId"
                      v-if="isLevelId"
                    >
                      <a-select
                        v-model:value="form.flowDesignId"
                        allowClear
                        :filter-option="filterOption"
                        show-search
                      >
                        <a-select-option
                          :value="item.id"
                          :title="item.name"
                          v-for="item in businessType"
                          >{{ item.name }}</a-select-option
                        >
                      </a-select>
                    </a-form-item>
                    <a-form-item
                      label="客户编码"
                      name="customerCode"
                      v-if="examine || isupData"
                    >
                      <a-input v-model:value="form.customerCode" allowClear />
                    </a-form-item>
                    <a-form-item
                      label="邀请码"
                      name="invitationCode"
                      v-if="examine || isupData"
                    >
                      <a-input v-model:value="form.invitationCode" allowClear />
                    </a-form-item>
                    <a-form-item
                      label="邮箱地址"
                      name="email"
                      :rules="[
                        {message: '请填写正确格式的邮箱', type: 'email'}
                      ]"
                      v-if="examine || isupData"
                    >
                      <a-input v-model:value="form.email" allowClear />
                    </a-form-item>
                    <a-form-item
                      label="客服电话"
                      name="customerServiceTel"
                      v-if="examine || isupData"
                    >
                      <a-input
                        v-model:value="form.customerServiceTel"
                        allowClear
                      />
                    </a-form-item>
                    <a-form-item
                      label="信用敞口金额"
                      name="creditAmount"
                      v-if="examine || isupData"
                    >
                      <a-input-number
                        style="width: 240px"
                        v-model:value="form.creditAmount"
                        :controls="false"
                        disabled
                      />
                    </a-form-item>
                    <!-- <a-form-item label="会员协议价折扣" name="discount" v-if="examine || isupData">
                  <a-input-number style="width: 240px" v-model:value="form.discount" :controls="false" disabled
                    addon-after="%" />
                </a-form-item> -->
                    <a-form-item
                      label="是否开启授信支付"
                      name="isCredit"
                      v-if="examine || isupData"
                    >
                      <a-radio-group
                        v-model:value="form.isCredit"
                        :disabled="!isCreditLine"
                        name="isCredit"
                      >
                        <a-radio :value="true">是</a-radio>
                        <a-radio :value="false">否</a-radio>
                      </a-radio-group>
                    </a-form-item>
                    <a-form-item
                      label="是否指定全商品"
                      name="isAllProduct"
                      v-if="examine || isupData"
                    >
                      <a-radio-group
                        disabled
                        v-model:value="chooseProductsData.isAllProduct"
                        name="isAllProduct"
                      >
                        <a-radio :value="true">是</a-radio>
                        <a-radio :value="false">否</a-radio>
                      </a-radio-group>
                    </a-form-item>
                    <a-form-item
                      label="协议价折扣"
                      v-if="
                        chooseProductsData.isDiscount &&
                        chooseProductsData.isAllProduct &&
                        (examine || isupData)
                      "
                    >
                      <a-input-number
                        disabled
                        style="width: 240px"
                        min="0"
                        v-model:value="chooseProductsData.discount"
                        :controls="false"
                        addon-after="%"
                      />
                    </a-form-item>
                    <a-form-item
                      label="协议价固定金额"
                      v-if="
                        !chooseProductsData.isDiscount &&
                        chooseProductsData.isAllProduct &&
                        (examine || isupData)
                      "
                    >
                      <a-input-number
                        disabled
                        style="width: 240px"
                        min="0"
                        v-model:value="chooseProductsData.discount"
                        :controls="false"
                        addon-after="元"
                      />
                    </a-form-item>
                  </a-col>
                </a-row>
                <a-form-item
                  label="详细地址"
                  name="address"
                  :labelCol="{span: 3}"
                  v-if="examine || isupData"
                >
                  <a-input v-model:value="form.address" disabled />
                </a-form-item>
                <div
                  v-if="
                    !chooseProductsData.isAllProduct && (examine || isupData)
                  "
                  style="margin-bottom: 10px"
                >
                  <a-table
                    size="small"
                    :dataSource="chooseProductsData.productInfoDtos"
                    :columns="columns1"
                    bordered
                    :pagination="false"
                  >
                    <template #bodyCell="{text, record, index, column}">
                      <template v-if="column.title == '产品图片'">
                        <div
                          class="pointer"
                          v-if="
                            record.detailUrls &&
                            record.detailUrls.length &&
                            record.detailUrls[0].ImgUrl
                          "
                          @click="
                            () => {
                              setVisible(true, index);
                            }
                          "
                        >
                          <img
                            :src="record.detailUrls[0].ImgUrl"
                            alt=""
                            style="height: 30px"
                          />
                          <a-image
                            :width="0"
                            :style="{display: 'none'}"
                            :preview="{
                              visible: visibleImg[index],
                              onVisibleChange: (e) => {
                                setVisible(e, index);
                              }
                            }"
                            :src="record.detailUrls[0].ImgUrl"
                          />
                        </div>
                      </template>
                      <template v-if="column.title == '计算方式'">
                        <span v-if="text">折扣</span>
                        <span v-else>固定价</span>
                      </template>
                      <template v-if="column.title == '协议价'">
                        {{ text }}{{ record.isDiscount ? "%" : "元" }}
                      </template>
                    </template>
                  </a-table>
                </div>
                <!-- <div
              style="display: flex; padding-left: 50px; margin-bottom: 20px"
              v-if="examine || isupData"
            >
              <span style="line-height: 30px">城市选择:&nbsp;&nbsp;</span>
              <a-select
                v-model:value="form.provinceId"
                :filter-option="filterOption"
                show-search
                allowClear
                style="width: 200px"
                placeholder="请选择省份"
                @change="
                  (a) => {
                    changeCity(a, 'province');
                  }
                "
              >
                <a-select-option
                  :value="item.code"
                  v-for="(item, index) in district"
                  :key="index"
                  :title="item.name"
                >
                  {{ item.name }}
                </a-select-option>
              </a-select>
              <a-select
                v-model:value="form.cityId"
                :filter-option="filterOption"
                show-search
                allowClear
                style="width: 200px; margin-left: 14px"
                placeholder="请选择城市"
                @change="
                  (a) => {
                    changeCity(a, 'city');
                  }
                "
              >
                <a-select-option
                  :value="item.code"
                  v-for="(item, index) in provinceList"
                  :key="index"
                  :title="item.name"
                >
                  {{ item.name }}
                </a-select-option>
              </a-select>
              <a-select
                v-model:value="form.countySN"
                :filter-option="filterOption"
                show-search
                allowClear
                style="width: 200px; margin-left: 14px"
                placeholder="请选择区县"
                @change="
                  (a) => {
                    changeCity(a, 'district');
                  }
                "
              >
                <a-select-option
                  :value="item.code"
                  v-for="(item, index) in cityList"
                  :key="index"
                  :title="item.name"
                >
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </div> -->
              </a-form>
              <div style="display: flex; justify-content: end" v-if="!examine">
                <a-button style="margin-right: 14px" @click="addFormData(1)"
                  >取消</a-button
                >
                <a-button type="primary" @click="addFormData(2)">确定</a-button>
              </div>
            </a-tab-pane>
            <a-tab-pane
              :key="2"
              tab="审批流程"
              v-if="examine && form.flowDesignId"
            >
              <ExamineApprove
                :parameter="form"
                ref="ExamineApproveRef"
                @ok="approveOk"
              ></ExamineApprove>
            </a-tab-pane>
            <a-tab-pane
              :key="3"
              tab="审批模板"
              v-if="examine && form.flowDesignId"
            >
              <div
                v-global-loading="loadingFlowNode"
                style="position: relative"
              >
                <div
                  style="
                    opacity: 0;
                    position: absolute;
                    width: 100%;
                    height: 100%;
                    z-index: 999;
                  "
                ></div>
                <FlowNode v-model:node="designContent"></FlowNode>
              </div>
            </a-tab-pane>
          </a-tabs>
        </div>
      </a-modal>
      <!-- 选择标签 -->
      <SelectLabel
        ref="selectLabelRef"
        v-model:list="tagList"
        :type="1"
        @ok="tagOk"
      ></SelectLabel>
      <!-- 设置协议价 -->
      <ChooseProducts ref="chooseProductsRef" v-model:data="chooseProductsData">
      </ChooseProducts>
    </div>
  </div>
</template>

<script lang="ts" setup>
import {ref, inject, onActivated, onMounted, computed} from "vue";
import {exportData} from "@/utils/common";
import {userFlowDesignLis} from "@/api/common";
import Pagination from "@/components/pagination/Pagination.vue";
import useAppStore from "@/store/app";
import ExamineApprove from "@/components/functional/ExamineApprove.vue";
import FlowNode from "@/components/flow/FlowNode.vue";
import {exportExcel} from "@/utils/exportFile";
import SelectLabel from "@/components/functional/selectLabel.vue";
import ChooseProducts from "@/components/functional/chooseProducts.vue";
import {
  membersInfoUpdate,
  membersInfoPage,
  membersInfoUpdateDiscount,
  membersInfoUpdateLevel,
  membersInfoDetail,
  membersInfoDelete,
  membersInfoChangeStatus,
  membersInfoList,
  membersInfoExportMembers,
  membersInfoUpdateCredit,
  membersInfoDiscountDetail,
  membersInfoExportMemberDiscount,
  membersInfoCheckPage,
  membersInfoChangeApplyStatus
} from "@/api/customerArchives/memberManagement";
import {flowdesignDetail} from "@/api/configCenter/platform/processTemplate";
import {membersTagsBatchCreate} from "@/api/customerArchives/membersTags";
import {userLevelList} from "@/api/customerArchives/userGrade";
import {message, Modal} from "ant-design-vue";
import district from "@/utils/city";
import {tagsInfoList} from "@/api/customerArchives/memberTag";
const ExamineApproveRef = ref();
const activeKey2 = ref(1);
const loadingFlowNode = ref(false);
const designContent = ref({});
//是否已经访问过
const interview = ref({
  a: false,
  b: false
});
const changeTab2 = (e) => {
  if (e == 2) {
    if (!interview.value.a) {
      setTimeout(() => {
        ExamineApproveRef.value.showModal();
        interview.value.a = true;
      }, 500);
    }
  } else if (e == 3) {
    if (!interview.value.b) {
      loadingFlowNode.value = true;
      flowdesignDetail({id: form.value.flowDesignId})
        .then((res) => {
          designContent.value = JSON.parse(res.data.designContent);
        })
        .finally(() => {
          loadingFlowNode.value = false;
        });
      interview.value.b = true;
    }
  }
};
const activeKey = ref(1);
const props = defineProps({
  type: {
    type: Number,
    default: 1
  }
});
const pageType = computed({
  get() {
    return props.type;
  },
  set() {}
});
const changeTab = (e) => {
  activeKey.value = e;
  pageSet.value = {
    pageNo: 1,
    pageSize: 20,
    total: 0,
    checkType: activeKey.value
  };
  loadPagination(true);
};
//审核
const approveOk = (data) => {
  let dataList = {
    id: data.id,
    businessType: 300,
    approvalFlowId: data.approvalFlowId,
    nodeId: data.nodeId,
    bizCheckOperationType: data.type,
    remark: data.remark
  };
  ExamineApproveRef.value.loading = true;
  membersInfoChangeApplyStatus(dataList)
    .then(() => {
      message.success("审核成功!");
      loadPagination(false);
      ExamineApproveRef.value.loading = false;
      visible.value = false;
    })
    .catch(() => {
      ExamineApproveRef.value.loading = false;
    });
};
const setVisible = (flag, index) => {
  visibleImg.value[index] = flag;
};
const visibleImg = ref<any>([]);
const filterOption = (input: string, option: any) => {
  return option.title.toLowerCase().indexOf(input.toLowerCase()) >= 0;
};
const calcCanUseHeight: any = inject("calcCanUseHeight");
onActivated(() => {
  calcCanUseHeight();
});
onMounted(() => {
  calcCanUseHeight();
});
//已选中标签集合
const tagList = ref<any>([]);
//已选中商品集合
const commodityList = ref<any>([]);
const chooseProductsRef = ref();
//当前操作的会员ID
const memberID = ref<any>();
const title = ref("详情");
const form = ref<any>({});
const visible = ref<boolean>(false);
const visible1 = ref<boolean>(false);
const userList = ref<Array<any>>([]);
const tableLoading = ref<boolean>(false);
const loading = ref<boolean>(false);
const modalloading = ref(false);
const tableData = ref<Array<object>>([]);
const selectLabelRef = ref();
//审批流程
const businessType = ref<any>([]);
const getBusinessType = () => {
  userFlowDesignLis({businessType: 300}).then((res) => {
    businessType.value = res.data;
  });
};
const pageSet = ref<any>({
  pageNo: 1,
  pageSize: 20,
  total: 0,
  checkType: activeKey.value
});
//是否是查看
const examine = ref<boolean>(false);
//是否是编辑
const isupData = ref<boolean>(false);
//是否是设置协议价
const isDiscount = ref<boolean>(false);
//是否是设置会员等级
const isLevelId = ref<boolean>(false);
//是否是授信设置
const isCreditLine = ref<boolean>(false);
//是否是设置标签
const isTags = ref<boolean>(false);
//会员协议价详情
const chooseProductsData: any = ref<object>({});
//选中标签回调
const tagOk = () => {
  selectLabelRef.value.modalloading = true;
  let tagIdStr = tagList.value.join(",");
  membersTagsBatchCreate({membersId: memberID.value, tagIds: tagIdStr})
    .then((res) => {
      message.success("设置成功");
      selectLabelRef.value.nodeModal();
      loadPagination();
    })
    .finally(() => {
      selectLabelRef.value.modalloading = false;
    });
};
//操作
const compile = (
  type: Number,
  id: number | string,
  status?: number,
  tagIds?: string
) => {
  if (type == 1) {
    //查看
    replacement();
    loading.value = true;
    examine.value = true;
    title.value = "详情";
    getChooseProductsData(id);
    activeKey2.value = 1;
    particulars(id);
  } else if (type == 2) {
    //禁用
    loading.value = true;
    let a = status == 0 ? 1 : 0;
    membersInfoChangeStatus({id, status: a})
      .then((res) => {
        message.success("修改成功");
        loadPagination();
      })
      .finally(() => {
        loading.value = false;
      });
  } else if (type == 3) {
    //编辑
    title.value = "编辑";
    replacement();
    loading.value = true;
    isupData.value = true;
    activeKey2.value = 1;
    particulars(id);
    getChooseProductsData(id);
  } else if (type == 4) {
    //设置协议价
    loading.value = true;
    membersInfoDiscountDetail({id})
      .then((res) => {
        res.data.productInfoDtos = res.data.productInfoDtos
          ? res.data.productInfoDtos
          : [];
        chooseProductsData.value = res.data;
        chooseProductsRef.value.showModal();
      })
      .finally(() => {
        loading.value = false;
      });
  } else if (type == 5) {
    //设置等级
    title.value = "设置等级";
    getBusinessType();
    replacement();
    isLevelId.value = true;
    activeKey2.value = 1;
    particulars(id);
  } else if (type == 6) {
    //授信设置
    title.value = "授信设置";
    replacement();
    isCreditLine.value = true;
    activeKey2.value = 1;
    particulars(id);
  } else if (type == 7) {
    selectLabelRef.value.showModal();
    memberID.value = id;
    tagList.value = tagIds?.split(",");
  } else if (type == 8) {
    //导出会员协议信息
    loading.value = true;
    membersInfoExportMemberDiscount({id})
      .then((res) => {
        exportData(res, "协议信息");
      })
      .finally(() => {
        loading.value = false;
      });
  } else if (type == 9) {
    //快捷跳转到审批流程
    replacement();
    loading.value = true;
    examine.value = true;
    title.value = "详情";
    getChooseProductsData(id);
    activeKey2.value = 2;
    particulars(id, true);
  }
};
//获取会员协议价详情
const getChooseProductsData = async (id) => {
  let res = await membersInfoDiscountDetail({id});
  res.data.productInfoDtos = res.data.productInfoDtos
    ? res.data.productInfoDtos
    : [];
  res.data.productInfoDtos.forEach((item: any) => {
    if (item.detailUrls) {
      item.detailUrls = JSON.parse(item.detailUrls) || [];
    } else {
      item.detailUrls = [];
    }
  });
  chooseProductsData.value = res.data;
};
//提交
const addFormData = (type: number) => {
  if (type == 1) {
    //关闭弹窗
    visible.value = false;
  } else if (type == 2) {
    //提交数据
    modalloading.value = true;
    if (isupData.value) {
      //编辑
      membersInfoUpdate(form.value)
        .then((res) => {
          message.success("编辑成功");
          visible.value = false;
          loadPagination();
        })
        .finally(() => {
          modalloading.value = false;
        });
    } else if (isDiscount.value) {
      //设置协议价
      membersInfoUpdateDiscount(form.value)
        .then((res) => {
          message.success("编辑成功");
          visible.value = false;
          loadPagination();
        })
        .finally(() => {
          modalloading.value = false;
        });
    } else if (isLevelId.value) {
      //设置会员等级
      let formData = {
        id: form.value.id,
        levelId: form.value.levelId,
        levelName: form.value.levelName,
        applyStatus: 1,
        flowDesignId: form.value.flowDesignId
      };
      if (!formData.levelId) {
        modalloading.value = false;
        return message.error("请选择会员等级");
      }
      if (!formData.flowDesignId) {
        modalloading.value = false;
        return message.error("请选择审批流程");
      }
      membersInfoUpdateLevel(formData)
        .then((res) => {
          message.success("提交成功");
          visible.value = false;
          loadPagination();
        })
        .finally(() => {
          modalloading.value = false;
        });
    } else if (isCreditLine.value) {
      //授信设置
      membersInfoUpdateCredit(form.value)
        .then((res) => {
          message.success("编辑成功");
          visible.value = false;
          loadPagination();
        })
        .finally(() => {
          modalloading.value = false;
        });
    }
  }
};
//获取会员等级信息
const getUserList = () => {
  userLevelList().then((res) => {
    userList.value = res.data;
  });
};
getUserList();
//获取会员标签
const tagDataList = ref<any>([]);
const getTagList = () => {
  tagsInfoList({tagType: 1}).then((res) => {
    tagDataList.value = res.data;
  });
};
getTagList();
// 审批状态
const applyStatusOptions = [
  {
    label: "未提交",
    value: 0
  },
  {
    label: "审核中",
    value: 1
  },
  {
    label: "审核驳回",
    value: 2
  },
  {
    label: "撤回",
    value: 3
  },
  {
    label: "审核通过",
    value: 9
  }
];
//获取数据
const loadPagination = (flag: boolean = false): void => {
  if (flag) {
    pageSet.value.pageNo = 1;
  }
  tableLoading.value = true;
  if (pageType.value == 1) {
    //查看界面
    membersInfoPage(pageSet.value)
      .then((res) => {
        tableData.value = res.data.rows;
        pageSet.value.total = res.data.totalRows;
      })
      .finally(() => {
        tableLoading.value = false;
      });
  } else {
    //审核等级界面
    membersInfoCheckPage(pageSet.value)
      .then((res) => {
        tableData.value = res.data.rows;
        pageSet.value.total = res.data.totalRows;
      })
      .finally(() => {
        tableLoading.value = false;
      });
  }
};
loadPagination(true);
//重置
const reset = (): void => {
  pageSet.value = {
    pageNo: 1,
    pageSize: 20,
    total: 0,
    checkType: activeKey.value
  };
  loadPagination(true);
};
//详情
const particulars = (id: string | number, flag = false) => {
  loading.value = true;
  interview.value = {
    a: false,
    b: false
  };
  membersInfoDetail({id})
    .then((res) => {
      form.value = res.data;
      form.value.levelId = form.value.levelId == 0 ? null : form.value.levelId;
      form.value.address =
        form.value.province +
          form.value.city +
          form.value.county +
          form.value.address || "";
      changeCityDetail();
      visible.value = true;
      if (activeKey.value == 1 && pageType.value == 2) {
        form.value.examine = true;
      } else {
        form.value.examine = false;
      }
      if (flag) {
        setTimeout(() => {
          ExamineApproveRef.value.showModal();
          interview.value.a = true;
        }, 500);
      }
    })
    .finally(() => {
      loading.value = false;
    });
};
//导出
const exportDerive = (): void => {
  loading.value = true;
  membersInfoExportMembers(pageSet.value)
    .then((res) => {
      exportData(res, "会员信息");
    })
    .finally(() => {
      loading.value = false;
    });
};
//重置状态
const replacement = () => {
  examine.value = false;
  isupData.value = false;
  isDiscount.value = false;
  isLevelId.value = false;
  isCreditLine.value = false;
};
const columns = ref<Array<object>>([
  {
    title: "序号",
    dataIndex: "name",
    align: "center",
    width: 50,
    customRender: ({index}) => {
      return index + 1;
    },
    fixed: "left"
  },
  {
    title: "默认集团公司名称",
    dataIndex: "parentCompany",
    align: "center",
    fixed: "left",
    width: 180
  },
  {
    title: "首次认证公司名称",
    dataIndex: "bodyName",
    align: "center",
    fixed: "left",
    width: 180
  },
  {
    title: "会员用户名",
    dataIndex: "name",
    align: "center",
    width: 120
  },
  {
    title: "账户",
    dataIndex: "account",
    align: "center",
    width: 120
  },
  {
    title: "会员手机号",
    dataIndex: "phone",
    align: "center",
    width: 120
  },
  {
    title: "会员标签",
    dataIndex: "tags",
    align: "center",
    width: 180
  },
  {
    title: "会员等级",
    dataIndex: "levelName",
    align: "center",
    width: 120
  },

  {
    title: "会员主体数量",
    dataIndex: "bodyNum",
    align: "center",
    width: 120
  },
  {
    title: "会员注册时间",
    dataIndex: "createdTime",
    align: "center",
    width: 180
  },
  {
    title: "邀请码",
    dataIndex: "invitationCode",
    align: "center",
    width: 180
  },
  {
    title: "会员状态",
    dataIndex: "status",
    align: "center",
    width: 120,
    customRender: ({value}) => {
      if (value == 1) {
        return "启用";
      } else {
        return "停用";
      }
    }
  },
  {
    title: "会员等级申请状态",
    dataIndex: "applyStatus",
    align: "center",
    width: 150,
    fixed: "right"
  },
  {
    title: "操作",
    dataIndex: "caozuo",
    align: "center",
    width: 170,
    fixed: "right"
  }
]);
//省市区三级联动
const provinceList: any = ref([]); //市
const cityList: any = ref([]); //区
const changeCity = (a, b) => {
  if (b == "province") {
    form.value.cityId = null;
    form.value.city = null;
    form.value.countySN = null;
    form.value.county = null;
    provinceList.value = [];
    cityList.value = [];
    if (a) {
      district.forEach((item) => {
        if (item.code == a) {
          provinceList.value = item.children;
          form.value.provinceId = item.code;
          form.value.province = item.name;
        }
      });
    } else {
      form.value.provinceId = null;
      form.value.province = null;
    }
  } else if (b == "city") {
    cityList.value = [];
    form.value.countySN = null;
    form.value.county = null;
    if (a) {
      provinceList.value.forEach((item) => {
        if (item.code == a) {
          cityList.value = item.children;
          form.value.cityId = item.code;
          form.value.city = item.name;
        }
      });
    } else {
      form.value.cityId = null;
      form.value.city = null;
    }
  } else if (b == "district") {
    if (a) {
      cityList.value.forEach((item) => {
        if (item.code == a) {
          form.value.countySN = item.code;
          form.value.county = item.name;
        }
      });
    } else {
      form.value.countySN = null;
      form.value.county = null;
    }
  }
};
const changeCityDetail = () => {
  for (let i = 0; i < district.length; i++) {
    if (district[i].code == form.value.provinceId) {
      provinceList.value = district[i].children;
      break;
    }
  }
  for (let i = 0; i < provinceList.value.length; i++) {
    if (provinceList.value[i].code == form.value.cityId) {
      cityList.value = provinceList.value[i].children;
      break;
    }
  }
};
const transfer = (
  name1: string,
  name2: string,
  data1: string | number,
  data2: string | number
) => {
  form.value[name1] = data1;
  form.value[name2] = data2;
};
const transfer1 = (e: any, name1: string, name2: string) => {
  if (e == undefined) {
    form.value[name1] = null;
    form.value[name2] = null;
  }
};
//登录方式
const loginList = ref([
  {type: 1, name: "普通登录"},
  {type: 2, name: "手机号"},
  {type: 3, name: "微信"},
  {type: 4, name: "注册"}
]);
const columns1 = ref<Array<any>>([
  {
    title: "序号",
    dataIndex: "index",
    align: "center",
    width: 50,
    customRender: ({index}) => {
      return index + 1;
    },
    fixed: "left"
  },
  {
    title: "产品名称",
    dataIndex: "productName",
    align: "center",
    width: 200
  },
  {
    title: "产品图片",
    dataIndex: "detailUrls",
    align: "center",
    width: 200
  },
  {
    title: "产品编码",
    dataIndex: "productCode",
    align: "center",
    width: 120
  },
  {
    title: "计算方式",
    dataIndex: "isDiscount",
    align: "center",
    width: 160
  },
  {
    title: "协议价",
    dataIndex: "discount",
    align: "center",
    width: 160
  }
]);
</script>

<style lang="less" scoped></style>
